<template>
  <div class="login" :style="'background-image:url('+ Background +');'">
    <el-form ref="form" :rules="rules" :model="form" label-position="left" label-width="0px"
         class="login-form">
      <h3 class="title">
        BOOKS_ADMIN
      </h3>
      <el-form-item prop="username">
        <el-input v-model="form.username" type="text" auto-complete="off" placeholder="账号">
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input icon="el-icon-user" v-model="form.password" type="password" auto-complete="off"
              placeholder="密码" @keyup.enter.native="handleLogin">
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input v-model="form.code" auto-complete="off" placeholder="验证码" style="width: 63%"
              @keyup.enter.native="handleLogin">
        </el-input>
        <div class="login-code">
          <img :src="'data:image/png;base64,'+codeUrl" @click="getCode">
        </div>
      </el-form-item>
      <el-checkbox v-model="form.rememberMe" style="margin:0 0 25px 0;">
        记住我
      </el-checkbox>
      <el-form-item style="width:100%;">
        <el-button size="medium" type="primary" style="width:100%;"
               @click.native.prevent="loginForm('form')">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

  import Background from '@/assets/image/login_background.jpeg'
  import {getCodeImg,login} from '@/api/user/login'

  export default {
    data() {
      return {
        Background,
        codeUrl: '',
        form:{
          username: 'admin',
          password: '123456',
          rememberMe: false,
          code: '',
          uuid: ''
		},
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '请输入验证码', trigger: 'blur' }
          ],
        },
        loading: false,
        redirect: undefined
      }
    },
    created() {
      // 获取验证码
      this.getCode()
    },
    methods: {
      loginForm() {
        if(this.loading){
          return false
		}
        this.loading = true;
        this.$refs.form.validate((valid) => {
          if (valid) {
            let loginParams = { username: this.form.username, password: this.form.password,
              code:this.form.code,uuid:this.form.uuid};
            login(loginParams).then(res=>{
              if(res.code === 200) {
                var Params = {
                  token: res.data.token,
                  username: this.loginForm.username,
                  name: res.data.realName,
                  userId: res.data.userId
                }
                this.$store.commit('LOGIN_IN', Params);
                this.$router.push({path: '/index'});
              }
			})
            this.loading = false;
          } else {
            this.loading = false;
            return false;
          }
        });
      },
      getCode() {
        getCodeImg().then(res => {
          this.codeUrl = res.data.img
          this.form.uuid = res.data.uuid
        })
      }
    }
  }
</script>

<style scoped>
    .login {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100%;
        width: 100%;
        top: 0px;
        left: 0px;
        position: absolute;
        background-size: cover;
        margin: 0;
    }
	.login-code{
        float: right;
	}
	.login-form{
		border-radius: 15px;
		margin: 180px;
		padding: 15px 35px;
		background: aliceblue;
		box-shadow: 0 0 25px #cac6c6;
	}
</style>
